package com.serena.da.rest.common;

import java.util.HashMap;

/**
 * Created by Spitkovskyi Igor on 7/29/2014.
 */
public class GlobalContext {
    public static final String GLOBAL_ENV_PREFIX = "GlobalEnv_";
    public static final String COMPT_PREFIX = "Component_";
    public static final String COMPT_PROCESS_PREFIX = "ComponentProcess_";
    public static final String APP_PREFIX = "App_";
    public static final String APP_PROCESS_PREFIX = "AppProcess_";
    public static final String APP_PROC_FLOW_PREFIX = "AppProcessFlow_";
    public static final String COMPT_PROC_FLOW_PREFIX = "ComponentProcFlow_";

    public static HashMap<String, Object> TestRunObjects;  //!!! This must be PRIVATE, fix when debug is finisehd
    public static boolean isInitialized = false;

    //private HashMap<String, Object> BackupContext;
    private String innerKey;
    private Object innerValue;

    /*public GlobalContext(HashMap<String, Object> newSet){

    }*/

    public GlobalContext(String newKey, Object newValue) {
        innerKey = newKey;          //backup initial key
        innerValue = Get(newKey);   //backup initial value
        Put(newKey, newValue);
    }

    /*private void backupInitialSet(HashMap<String, Object> newSet){
        BackupContext = new HashMap<String, Object>();
        Iterator itr = BackupContext.entrySet().iterator();
        while(itr.hasNext()){
            //put existing values into BackupContext
        }
        put(newSet);  //Implement put method accepting HashMap as argument.
    }*/


    public void Dispose() {
        if (!innerKey.isEmpty()) {
            Put(innerKey, innerValue);
        }
    }

    ////////////////////////////////////////////////////////////////////////////
    public static void Put(String key, Object value) {
        if (TestRunObjects == null) {
            TestRunObjects = new HashMap<>();
            isInitialized = true;
        }

        if (TestRunObjects.containsKey(key)) {
            System.out.println("Provided key already exists in the GlobalContext container. Coresponding value will be replaced.");
        }

        if (key == null || value == null) {
            //throw new Exception ("Key or value provided to be put into GlobalContext is null.");
            System.out.println("Key or value provided to be put into GlobalContext is null.");
        }

        TestRunObjects.put(key, value);
    }

    public static Object Get(String key) {
        if (isInitialized && TestRunObjects.containsKey(key)) {
            return TestRunObjects.get(key);
        }
        return null;
    }


    //Addressed getters
    public static String getGlobalEnvID(String GlobalEnvName) {
        return Get(GLOBAL_ENV_PREFIX + GlobalEnvName).toString();
    }

    public static String getComponentID(String ComponentName) {
        return Get(COMPT_PREFIX + ComponentName).toString();
    }

    public static String getComponentProcessID(String ComponentProcessName) {
        return Get(COMPT_PROCESS_PREFIX + ComponentProcessName).toString();
    }

    public static String getApplicationID(String ApplicationName) {
        return Get(APP_PREFIX + ApplicationName).toString();
    }

    public static String GetApplicationProcessID(String ApplicationProcessName) {
        return Get(APP_PROCESS_PREFIX + ApplicationProcessName).toString();
    }

}
